package engine.newUi
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import newx.StageInfo;
	import newx.com.config.StaticIni;

	/**
	 * 打坐加速提示逻辑 
	 * @author liuel
	 * 
	 */	
	public class SitAlerLogic
	{
		private static  var _self :SitAlerLogic;
		public          var _ui   :SitAlerUI;
		
		private var _func :Function;
		//
		private var _funcNoClick:Function; 
		
		private var _fucOther:Function;
		private var _isNoClick:Boolean=false; 
		
		
		//点击的按钮
		private var _target:Object;
		/**不记住状态**/		
		private const REMEED_STATE		:int =	1;
		/**选择状态**/
		private const NOREMEED_STATE	:int =	2;
		//保存每个UI的状态
		private var stateArr:Vector.<uiState>;
		
		public static function getIntatic():SitAlerLogic
		{
			if(_self == null)
			{
				_self = new SitAlerLogic();
			}
			return _self;
		}
		public function SitAlerLogic():void
		{
			
		}
	    public function Init():void 
		{
			_ui         = SitAlerUI.getInstance();
			stateArr	= new Vector.<uiState>();
			_ui.checkBtn.stop();
		}
		
		public function AfterHide():void
		{
			removeEvent();
		}
		
		public function BeforeShow():void
		{
		
			addEvent();
		}
		private function getState():Boolean
		{	
			for(var i:String in stateArr)
			{
				if(stateArr[i].target == _target)
				{
					return (stateArr[i]).isSelect;
				}
			}
			return false;
		}
		/**
		 * 设置信息 
		 * @param str 字符ID
		 * @param arr 数据
		 * 
		 */		
		private function setInfo( str:String,arr:Array):void
		{
			_ui.titleLabel.htmlText = StaticIni.getSwfLangStrVar(str,arr);
			
			//界面自适应
			var txtWidth : int = _ui.titleLabel.width;
			var nowWidth : int = txtWidth + _ui.titleLabel.x * 2;

			if( nowWidth < 350 )return;
			
			_ui.windowBG.width = nowWidth;
			
			_ui.checkBtn.x = ( _ui.windowBG.width - _ui.checkBtn.width - _ui.label2.width ) * .5;
			_ui.label2.x = _ui.checkBtn.x + _ui.checkBtn.width + 5;
			_ui.okBtn.x = _ui.windowBG.x + 50;
			_ui.noBtn.x = _ui.windowBG.width - 50 - _ui.noBtn.width;
			
		}
		private function addEvent():void
		{
		   _ui.okBtn.addEventListener(MouseEvent.CLICK,onClickOK);
		   _ui.noBtn.addEventListener(MouseEvent.CLICK,onClickNO);
		   _ui.btn.addEventListener(MouseEvent.CLICK,onClickbtn);
		   _ui.checkBtn.addEventListener(MouseEvent.CLICK,onClickCheck);
		}
		private function removeEvent():void
		{
			_ui.okBtn.removeEventListener(MouseEvent.CLICK,onClickOK);
			_ui.noBtn.removeEventListener(MouseEvent.CLICK,onClickNO);
			_ui.btn.removeEventListener(MouseEvent.CLICK,onClickbtn);
			_ui.checkBtn.removeEventListener(MouseEvent.CLICK,onClickCheck);
		}
	
		/////////////////////////////////////////////////////监听函数///////////////////////////////////////////////////////////////
		private function onClickbtn(e:MouseEvent):void
		{
			_fucOther();
			_ui.hide();
		}
		private function onClickOK(e:MouseEvent):void
		{
			_func();
			_ui.hide();
		}
		private function onClickNO(e:MouseEvent):void
		{
			if(_isNoClick==true)
			{
				_isNoClick= false; 
				_funcNoClick(); 
			}
			else
			{
				_ui.hide();
			}
		}
		public function set isNoClick(value:Boolean):void
		{
			_isNoClick=value ;
		}
		private function onClickCheck(e:MouseEvent):void
		{
			var uPassCheckBox:MovieClip=e.currentTarget as MovieClip;
			if(REMEED_STATE	==	uPassCheckBox.currentFrame	)
			{
				uPassCheckBox.gotoAndStop(NOREMEED_STATE);
				if(_Check==true)
				{
					_Check=false ; 
					_target["fit"]=NOREMEED_STATE//不记住状态
				}
				
			}
			else
			{
				uPassCheckBox.gotoAndStop(REMEED_STATE);
				if(_Check==true)
				{
					_Check=false ;
					_target["fit"]=REMEED_STATE //记住状态
				}
				
			}
			updateState(uPassCheckBox);
		}
		/**
		 *记住或者更新当前按钮选择的状态 
		 * 
		 */		
		private function updateState(mc:MovieClip):void
		{
			var index:int =-1;
			for(var i:String in stateArr)
			{
				if(stateArr[i].target == _target)
				{
					index = int(i);
				}
			}
			if(index>=0)
			{
				if(mc.currentFrame == NOREMEED_STATE)
				{
					stateArr[index].isSelect =true;
				}
				else if(mc.currentFrame ==REMEED_STATE)
				{
					stateArr[index].isSelect =false;
				}
			}
			else
			{
				if(mc.currentFrame == NOREMEED_STATE)
				{
					stateArr.push(new uiState(_target,true));
				}
				else if(mc.currentFrame ==REMEED_STATE)
				{
					stateArr.push(new uiState(_target,false));
				}
			}
		}
		/**
		 * 
		 * @param fun   确定的消息
		 * @param clickfun  点击的按钮
		 * 
		 */			
		private function  Func(fun:Function,clickBtn:Object,isCallBack:Boolean,funother:Function=null):void
		{
			_func = fun;
			_target=clickBtn 
			_fucOther =funother;
			if(getState())
			{
				if(isCallBack)
				{
					_func();
				}
			}
			else
			{
				_ui.show( StageInfo.systemSpace );
				_ui.checkBtn.gotoAndStop(1);
			}
		}
		/**
		 *调用记住提示框的接口 
		 * @param str  显示的字符串ID
		 * @param arr  显示的数据
		 * @param fun  确定发送的回调函数
		 * @param clickBtn 点击弹出提示的标示
		 * @param isHave 是否有需要第三个按钮
		 * @param func  点击第三个按钮的回调
		 * @param plist 第三个按钮名字
		 * 
		 */		
		public function setUIinfo(str:String,arr:Array,fun:Function,clickBtn:Object,isCallBack:Boolean=true,isHave:Boolean=false,func:Function=null,btnname:String=null):void
		{
			_isNoClick=false ; 
			SitAlerUI.getInstance().okBtn.label = StaticIni.getSwfLangStr("Common_OK");
			SitAlerUI.getInstance().noBtn.label = StaticIni.getSwfLangStr("Common_Cancel");
			setInfo(str,arr);
			if(func!=null)
			{
				Func(fun,clickBtn,isCallBack,func);
			}
			else
			{
				Func(fun,clickBtn,isCallBack);
			}
			if(!isHave)
			{
				_ui.btn.visible=false;
				_ui.okBtn.x=67;
				_ui.noBtn.x =197;
			}
			else
			{
				_ui.btn.label =btnname;
				_ui.btn.visible=true;
				_ui.okBtn.x =30;
				_ui.noBtn.x =132;
				_ui.btn.x= 237;
				
			}
		}
		public function setGiveUp(str:String,arr:Array,fun:Function,fun_1:Function ,noClick:Boolean=false,isHave:Boolean = false):void
		{
			_isNoClick=noClick; 
			setInfo(str,arr);
			_funcNoClick = fun ; 
			_func=fun_1 ; 
			if(!isHave)
			{
				_ui.btn.visible=false;
				_ui.okBtn.x=67;
				_ui.noBtn.x =197;
			}
			else
			{
//				_ui.btn.label =btnname;
				_ui.btn.visible=true;
				_ui.okBtn.x =30;
				_ui.noBtn.x =132;
				_ui.btn.x= 237;
				
			}
		}
		//记录对勾状态
		private var _Check:Boolean =false;
		public function setGiveUp_FB(str:String,arr:Array,fun:Function,fun_1:Function ,clickBtn:Object,noClick:Boolean=false,Check:Boolean=false):void
		{
			_Check = Check
			_isNoClick=noClick; 
			setInfo(str,arr);
			_funcNoClick = fun ; 
			_func=fun_1 ; 
			
			_ui.btn.visible=false;
			_ui.okBtn.x=67;
			_ui.noBtn.x =197;
			
			Func(fun_1,clickBtn,true,null);
		
		}
	}
}


class uiState
{
	/**
	 *是否记住选择 
	 */	
	public var isSelect:Boolean;
	public var target:Object;
	public function uiState(target:Object,isSelect:Boolean):void
	{
		this.isSelect = isSelect;
		this.target	   =target;
	}
}